package com.cn.hjsj.appInterface.appImpl;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cn.hjsj.appInterface.appService.AppStoreService;
import com.cn.hjsj.pojo.Store;
import com.cn.hjsj.service.IStoreService;
import com.cn.hjsj.util.GeoUitl;
import com.cn.hjsj.util.log.LogUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component("appStoreImpl")
public class AppStoreImpl implements AppStoreService{
	@Resource(name = "storeService")
	private IStoreService storeService;
/*
 * (non-Javadoc)
 * @see com.cn.hjsj.appInterface.appService.AppStoreService#getSite(net.sf.json.JSONObject)
 * author lutao
 * Date   2017-03-02
 * 获取门店列表
 */
	@Override
	public Map<String, Object> getSite(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object>  map=new HashMap<String,Object>(),
				mapOne=new HashMap<String,Object>(),
				maps=new HashMap<String,Object>(),
				mapTwo=new HashMap<String,Object>();
		JSONArray store = new JSONArray();
		double distance1 = 0;
		try{
			//获取 storeList 列表
			double jd = object.getDouble("longitudeY");
			double wd = object.getDouble("latitudeX");
			//距离
			long distance =4000;
			//计算最大最小的经纬度			
			double minlatitudeX = wd - GeoUitl.doLatDegress(distance);
			double maxlatitudeX = wd + GeoUitl.doLatDegress(distance);
			double minlongitudeY = jd - GeoUitl.doLngDegress(distance, wd);
			double maxlongitudeY = jd + GeoUitl.doLngDegress(distance, wd);
			//组织数据
			maps.put("minlatitudeX", minlatitudeX);
			maps.put("maxlatitudeX", maxlatitudeX);
			maps.put("minlongitudeY", minlongitudeY);
			maps.put("maxlongitudeY", maxlongitudeY);
			List<Store> storeList = storeService.getListByXY(maps);
			int count = 0;
					//storeService.getCountByXY(maps);
			
			if(storeList!=null){
				for (int i=0;i<storeList.size();i++){
					distance1 = GeoUitl.GetDistance(jd, wd, storeList.get(i).getLongitudey(), storeList.get(i).getLatitudex());
					//System.out.println(distance1);
					if(distance1 <=4000  ){
						mapOne.put("code", storeList.get(i).getCode());
						mapOne.put("name", storeList.get(i).getName());	
						mapOne.put("tel", storeList.get(i).getTel());	
						mapOne.put("address", storeList.get(i).getAddress());
						mapOne.put("distance", distance1);
						store.add(mapOne);
						count++;
					}
				}
				
				LogUtil.info("/AppStoreImpl/getSite:调用成功");
				mapTwo.put("siteList", store);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功");						
			}else{
				LogUtil.info("/AppStoreImpl/getSite:调用成功");
				mapTwo.put("serviceType", store);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功,数据库中无数据");
			}
			return map;
		}catch(Exception e){
			LogUtil.error("/AppStoreImpl/getSite:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
		
			return map;
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.cn.hjsj.appInterface.appService.AppStoreService#getOutlets(net.sf.json.JSONObject)
	 * author lutao
	 * Date   2017-03-03
	 * 获取门店列表
	 */
	@Override
	public Map<String, Object> getOutlets(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object>  map=new HashMap<String,Object>(),
				mapOne=new HashMap<String,Object>(),
				maps=new HashMap<String,Object>(),
				mapTwo=new HashMap<String,Object>();
		JSONArray store = new JSONArray();
		double distance = 0;
		try{
			//获取 storeList 列表
			double jd = object.getDouble("longitudeY");
			double wd = object.getDouble("latitudeX");
			Integer page = object.getInt("page");					
            Integer pageNo = object.getInt("linage");             		
            page = (page - 1) * pageNo;
            if(page<0){
                map.put("code", "30005");
                map.put("mes", "page有误");
                return map;
            }
            maps.put("startRow", page);
            maps.put("pageSize", pageNo);
            List<Store> storeList = storeService.getListByXY(maps);
            int count = storeService.getCountByXY(maps);
            if(storeList!=null){
				for (int i=0;i<storeList.size();i++){
						distance = GeoUitl.GetDistance(jd, wd, storeList.get(i).getLongitudey(), storeList.get(i).getLatitudex());
						mapOne.put("code", storeList.get(i).getCode());
						mapOne.put("name", storeList.get(i).getName());	
						mapOne.put("tel", storeList.get(i).getTel());	
						mapOne.put("address", storeList.get(i).getAddress());
						mapOne.put("distance", distance);
						store.add(mapOne);

					}
				LogUtil.info("/AppStoreImpl/getOutlets:调用成功");
				mapTwo.put("siteList", store);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功");						
			}else{
				LogUtil.info("/AppStoreImpl/getOutlets:调用成功");
				mapTwo.put("serviceType", store);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功,数据库中无数据");
			}
			return map;
            
		}catch(Exception e){
			LogUtil.error("/AppStoreImpl/getOutlets:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
		
			return map;
		}
	}
	
}
